package com.base.cn.platform.os.controller.mobile.global;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.exam.paper.ExamPaperService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * H5全局搜索结果页Controller
 */
@Controller
@RequestMapping("/mobile/globalSearch")
public class MobileGlobalSearchController extends MobileBaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private OpArticleService articleService;
    @Autowired
    private ExamPaperService examPaperService;

    /**
     * 搜索结果页
     */
    @RequestMapping("/globalSearchIndex")
    public String globalSearchIndex(Model model, @RequestParam("keyword") String keyword) {
        if (StringUtils.isNotEmpty(keyword)) {
            //判断是乱码 (GBK包含全部中文字符；UTF-8则包含全世界所有国家需要用到的字符。)
            if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(keyword))) {
                try {
                    keyword = new String(keyword.getBytes("ISO-8859-1"), "utf-8"); //转码UTF8
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        model.addAttribute("overallName", keyword);
        return resultPage("/globalSearch/global-search");
    }

    /**
     * 获取查询的课程数据
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchCourse")
    public String queryGroomDataList(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("courseNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (StringUtils.isNotEmpty(overallValue)) {
            dataList = courseService.findCourseList(condition, true, false, false, true);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-course-list");
    }

    /**
     * 获取查询的教师数据
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchTeacher")
    public String globalSearchTeacher(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> condition = new HashMap<>();
        condition.put("teacherNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (StringUtils.isNotEmpty(overallValue)) {
            dataList = sysTeacherService.findSysTeacherList(condition, false, false, false, false, false);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-teacher-list");
    }

    /**
     * 获取查询的试卷数据
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchExamPaper")
    public String globalSearchQuestion(HttpServletRequest request, Model model, @RequestParam("overallValue") String overallValue) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("paperNames", overallValue);
        params.put("userId", userId);
        params.put("examBuyStatusData", true);
        params.put("subjectData", true);
        params.put("newestData", true);
        params.put("status", 1);
        params.put("useType", 1);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (StringUtils.isNotEmpty(overallValue)) {
            dataList = examPaperService.findExamPaperList(params);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-examPaper-list");
    }

    /**
     * 获取查询的咨询数据
     *
     * @param model
     * @param overallValue
     * @return
     */
    @RequestMapping("/globalSearchArticle")
    public String globalSearchArticle(Model model, @RequestParam("overallValue") String overallValue) {
        Map<String, Object> params = new HashMap<>();
        params.put("articleNames", overallValue);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (StringUtils.isNotEmpty(overallValue)) {
            dataList = articleService.findOpArticleList(params);
        }
        model.addAttribute("dataList", dataList);
        return resultPage("/globalSearch/groom-search-article-list");
    }
}
